//
//  UIViewController+Navigation.m
//  YOHO Pay
//
//  Created by chuanshuangzhang  on 8/29/15.
//  Copyright (c) 2015 chuanshuangzhang . All rights reserved.
//

#import "UIViewController+Navigation.h"


@implementation UIViewController (Navigation)

- (void)setTitle:(NSString *)title back:(BOOL)isBack
{
    self.automaticallyAdjustsScrollViewInsets = NO;
    if ([self respondsToSelector:@selector(edgesForExtendedLayout)]){
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.backBarButtonItem = nil;
    self.navigationItem.leftBarButtonItem = nil;
    [self.navigationController.navigationBar setBackgroundImage:[Utils createImageWithColor:RGBACOLOR(236, 73,93, 1.0)] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.translucent = YES;
    if (isBack){
        UIButton *btn = [self createButtonWithImage:[UIImage imageNamed:@"nav_back_btn"]
                                        highlighted:[UIImage imageNamed:@"nav_back_btn"]
                                             action:@selector(leftBarButtonItemAction:)];
        CGFloat offset = ([[UIDevice currentDevice].systemVersion floatValue]<=7.0)?-36.0:-18.0f;
        [btn setContentEdgeInsets:UIEdgeInsetsMake(0,offset, 0, 0)];
        UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithCustomView:btn];
        self.navigationItem.leftBarButtonItem = leftItem;
        self.navigationItem.leftBarButtonItem.title = @"";
    }else{
        self.navigationItem.hidesBackButton = YES;
    }
    
    if (title && [title isKindOfClass:[NSString class]] && [title length]>0){
        UILabel *titleLbl = [[self class] createLable:CGRectMake(0, 0, 200, 44)];
        [titleLbl setTextAlignment:NSTextAlignmentCenter];
        titleLbl.textColor = [UIColor whiteColor];
        titleLbl.text = title;
        titleLbl.font=[UIFont boldSystemFontOfSize:18.0];
        titleLbl.backgroundColor = [UIColor clearColor];
        CGRect frame;
        CGFloat maxWidth = 60+35;
        frame = titleLbl.frame;
        frame.size.width = 320 - maxWidth * 2;
        titleLbl.frame = frame;
        self.navigationItem.titleView = titleLbl;
    }
}

- (void)setRightItem:(NSString *)tittle imageName:(NSString *)imageName
{
  [self setItemTitle:tittle image:imageName action:@selector(rightBarButtonItemAction:) right:YES];
}

-(void)setItemTitle:(NSString *)title
              image:(NSString *)imagename
             action:(SEL)sel
              right:(BOOL)isright{
    
    if(title.length<=0&&imagename.length<=0)
    {
        if(isright)
        {
            self.navigationItem.rightBarButtonItem = nil;
        }else {
            self.navigationItem.leftBarButtonItem = nil;
        }
        sel = nil;
    }
    UIButton *btn = [self createButtonWithImage:[UIImage imageNamed:imagename]
                                    highlighted:[UIImage imageNamed:[imagename stringByAppendingString:@"_hl"]]
                                         action:sel];
    CGFloat t = 44;
    if(title.length>3){
        t = title.length * 20;
    }
    if(title.length >1){
        [btn setFrame:CGRectMake(0,0,t, 44)];
        [btn setTitle:title forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];//RGB(255, 159, 182, 1.0)
        [btn.titleLabel setFont:[UIFont fontWithName:@"Arial" size:18]];
    }
    UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithCustomView:btn];
    if (isright) {
        [btn setContentEdgeInsets:UIEdgeInsetsMake(0,0,0,0)];
        self.navigationItem.rightBarButtonItem = item;
    }else{
        self.navigationItem.leftBarButtonItem = item;
    }
}

- (void)setLeftItem:(NSString *)title imageName:(NSString *)imageName
{
    [self setItemTitle:title image:imageName action:@selector(leftBarButtonItemAction:) right:NO];
}

- (UIButton *)createButtonWithImage:(UIImage *)image
                        highlighted:(UIImage *)hlimage
                             action:(SEL)sel
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setFrame:CGRectMake(0,0,44, 44)];
    [btn setImage:image forState:UIControlStateNormal];
    [btn setImage:hlimage forState:UIControlStateHighlighted];
    [btn setBackgroundColor:[UIColor clearColor]];
    [btn addTarget:self action:sel forControlEvents:UIControlEventTouchUpInside];
    return btn;
}

+(UILabel *)createLable:(CGRect)rect
{
    UILabel *lbl = [[UILabel alloc]initWithFrame:rect];
    lbl.backgroundColor = [UIColor clearColor];
    lbl.font = [UIFont systemFontOfSize:16.0];
    lbl.textColor = [UIColor clearColor];
    return lbl;
}

- (IBAction)leftBarButtonItemAction:(id)sender
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)rightBarButtonItemAction:(id)sender
{
    
}

@end
